package com.family.tools;

import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.Book;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.util.List;

import javax.print.PrintService;
import javax.print.PrintServiceLookup;

import com.family.domain.Order;
import com.family.domain.SubFood;

public class Print implements Printable {

    private List<SubFood> list;
    private String dnum;
    private String dname;

    public Print(List<SubFood> list,String dnum,String dname)
    {
        this.list = list;
        this.dname=dname;
        this.dnum = dnum;
    }

    @Override
    public int print(Graphics g, PageFormat pf, int page) throws PrinterException {

        if (page > 0) {
            return NO_SUCH_PAGE;
        }

        Graphics2D g2d = (Graphics2D) g;
        int height=20;

        g2d.setFont(new Font("Default", Font.PLAIN, 15));
        g2d.drawString(this.dnum+"号"+" "+this.dname, 0, height);
        height=height+20;

        g2d.setFont(new Font("Default", Font.PLAIN, 7));
        g2d.drawString("日期   "+DateFormatUtils.getNowTime(), 0, height);
        height=height+14;

        g2d.setFont(new Font("Default", Font.PLAIN, 14));
        g2d.drawString("--------------------------------------------------------------", 0, height);
        height=height+10;

        g2d.setFont(new Font("Default", Font.PLAIN, 10));
        g2d.drawString("编码   菜品名                 数量", 0, height);
        height=height+8;

        g2d.setFont(new Font("Default", Font.PLAIN, 7));
        g2d.drawString("---------------------------------------------------------------", 0, height);
        height=height+20;


        int i=0;

        for(SubFood food :this.list)
        {
            i++;
            //编号
            g2d.setFont(new Font("Default", Font.PLAIN, 9));
            g2d.drawString(" "+i+"", 0, height);


            //菜名
            g2d.setFont(new Font("Default", Font.PLAIN, 11));
            g2d.drawString("   "+food.getFoodname(), 5, height);

            //数量
            g2d.setFont(new Font("Default", Font.PLAIN, 11));
            g2d.drawString(food.getCount(), 110, height);

            height = height+20;
        }

        height = height+10;
        g2d.setFont(new Font("Default", Font.PLAIN, 20));
        g2d.drawString("----------------------------------", 0, height);
        height = height+10;

        g2d.setFont(new Font("Default", Font.PLAIN, 14));
        g2d.drawString("合计:    "+i, 0, height);
        height = height+80;

        g2d.setFont(new Font("Default", Font.PLAIN, 20));
        g2d.drawString("----------------------------------", 0, height);


        return PAGE_EXISTS;
    }

    public  void startPrint(Print print) {

        int height = 9000;

        // 通俗理解就是书、文档
        Book book = new Book();

        // 打印格式
        PageFormat pf = new PageFormat();
        pf.setOrientation(PageFormat.PORTRAIT);

        // 通过Paper设置页面的空白边距和可打印区域。必须与实际打印纸张大小相符。
        Paper p = new Paper();
        p.setSize(230, height);
        p.setImageableArea(0, -20, 130, height);
        pf.setPaper(p);

        // 把 PageFormat 和 Printable 添加到书中，组成一个页面
        book.append(print, pf);

        //默认打印机服务
        //PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();


        /*获取本机所有打印机服务*/
        PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null);
        if(services.length == 0){
            System.out.println("not found printer");
            return;
        }


        // 获取打印服务对象
        PrinterJob job = PrinterJob.getPrinterJob();

        int flag=0;
        for(int i=0;i< services.length;i++){
            PrintService ps = services[i];
            if(ps.getName().equals("food")){
                try {
                    job.setPrintService(ps);
                    flag=1;
                } catch (PrinterException e) {
                    e.printStackTrace();
                }
            }
        }

        if(flag==0)
        {
            System.out.println("no print service");
            return;
        }

        job.setPageable(book);
        try {
            job.print();
        } catch (PrinterException e) {
            System.out.println("================打印出现异常");
        }

    }

}